package com.traffic.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class StrUtil {
	
	/**
	 * @param targetClass     目标class
	 * @param fields	               需要转成json的 属性
	 * @param clazz	                         特殊的对象属性
	 * @param clazzFields     特殊的对象属性 需要转成json的属性
	 * @param object		     要转的内容
	 */
	public static void sendJson(Class targetClass,String[] fields,Class clazz,String[]  clazzFields,Object object) {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter print=null;
		try {
			print=response.getWriter();
			NormalStrategy ts = new NormalStrategy(targetClass);
			ts.setClazz(clazz);
			ts.setClazzFields(clazzFields);
			ts.setFields(fields);
			ts.setReverse(true);
			Gson gson = new GsonBuilder()
			.setDateFormat("yyyy-MM-dd HH:mm:ss") 
			.setExclusionStrategies(ts)
			.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY)
			.create();
			String jsonString  = gson.toJson(object);
			print.print(jsonString);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (response!=null) {
			    print.flush();
				print.close();
			}
		}
	}
	/**
	 * @param targetClass     目标class
	 * @param fields	               需要转成json的 属性
	 * @param clazz	                         特殊的对象属性
	 * @param clazzFields     特殊的对象属性 需要转成json的属性
	 * @param object		     要转的内容
	 */
	public static String paseJson(Class targetClass,String[] fields,Class clazz,String[]  clazzFields,Object object) {
		String jsonString = null;
		try {
			
			NormalStrategy ts = new NormalStrategy(targetClass);
			ts.setClazz(clazz);
			ts.setClazzFields(clazzFields);
			ts.setFields(fields);
			ts.setReverse(true);
			Gson gson = new GsonBuilder()
			.setDateFormat("yyyy-MM-dd HH:mm:ss") 
			.setExclusionStrategies(ts)
			.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY)
			.create();
			jsonString  = gson.toJson(object);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonString;
	}
	/**
	 * @param targetClass     目标class
	 * @param fields	               需要转成json的 属性
	 * @param object		     要转的内容
	 */
    public static void sendJson(Class targetClass,String[] fields,Object object){
    	HttpServletResponse response = ServletActionContext.getResponse();
    	response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter print = null;
		try {
			print=response.getWriter();
			NormalStrategy ts = new NormalStrategy(targetClass);
			ts.setFields(fields);
			ts.setReverse(true);
			Gson gson = new GsonBuilder()
			.setDateFormat("yyyy-MM-dd HH:mm:ss") 
			.setExclusionStrategies(ts)
			.registerTypeAdapterFactory(HibernateProxyTypeAdapter.FACTORY)
			.create();
			String jsonString  = gson.toJson(object);
			print.print(jsonString);
		} catch (IOException e) {
			e.printStackTrace();		
		}finally{
			print.close();
		}
		
    }
    /**
     * 
     * @param object ת��������
     */
    public static  void sendJson(Object object) {
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter print = null;
		try {
			print = response.getWriter();
			Gson gson = new GsonBuilder()  
			.setDateFormat("yyyy-MM-dd HH:mm:ss")  
			.create();
			String jsonString  = gson.toJson(object); 
			print.print(jsonString);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
